/**
 * Project:   dtu.fmes.googleloon.ui
 * File:      DrawController.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Nov 22, 2013
 */

package dtu.fmes.loon.ui.draw;

import java.awt.Dimension;

import javax.swing.JScrollPane;

import org.apache.log4j.Logger;

import dtu.fmes.loon.model.SimulationStateHistory;
import dtu.fmes.loon.ui.components.PaintJPanel;

/**
 * @author Bartosz Cichecki
 * 
 */
public class DrawController {

	private static final Logger log = Logger.getLogger(DrawController.class);

	private PaintJPanel canvas;

	private int currentFrame = 0;

	private SimulationStateHistory simulationStateHistory;

	private double zoomFactor = 0.01;

	private JScrollPane scrollableCanvas;

	private int zoomStep = 2;

	public DrawController(JScrollPane scrollableCanvas, PaintJPanel canvas, SimulationStateHistory simulationStateHistory) {
		this.scrollableCanvas = scrollableCanvas;
		this.canvas = canvas;
		this.simulationStateHistory = simulationStateHistory;
	}

	private boolean drawFrame() {
		if (log.isInfoEnabled()) {
			log.info("Drawing frame: " + currentFrame + ", zoom factor: " + zoomFactor);
		}

		scrollableCanvas.remove(canvas);

		int width = (int) (simulationStateHistory.getEnvironment().getArea().getxSize() * zoomFactor);
		int height = (int) (simulationStateHistory.getEnvironment().getArea().getySize() * zoomFactor);

		// if (log.isDebugEnabled()) {
		// log.debug("New canvas size: " + width + "," + height);
		// }

		canvas.setPreferredSize(new Dimension(width, height));
		scrollableCanvas.setViewportView(canvas);

		if (currentFrame >= simulationStateHistory.getStateHistory().size()) {
			currentFrame = simulationStateHistory.getStateHistory().size() - 1;
		}

		if (currentFrame < simulationStateHistory.getStateHistory().size()) {
			canvas.setSimulationData(simulationStateHistory, currentFrame, zoomFactor);
			canvas.repaint();
			return true;
		} else {
			canvas.repaint();
			return false;
		}
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public long getCurrentTime() {
		return getCurrentFrame() * simulationStateHistory.getSimulation().getSimulationInterval();
	}

	public int getFrameCount() {
		return simulationStateHistory.getStateHistory().size();
	}

	public double getZoomFactor() {
		return zoomFactor;
	}

	public boolean isLastFrame() {
		return currentFrame == simulationStateHistory.getStateHistory().size() - 1;
	}

	public void jumpToFrame(int frame) {
		currentFrame = frame;
		if (log.isInfoEnabled()) {
			log.info("Jumping to frame: " + currentFrame);
		}
		drawFrame();
	}

	public void rewindFrames(int frameCount) {
		currentFrame += frameCount;
		if (log.isInfoEnabled()) {
			log.info("Rewinding to frame: " + currentFrame);
		}
		drawFrame();
	}

	public void setCoverageOnlyMode(boolean enabled) {
		canvas.setCoverageOnlyMode(enabled);
		drawFrame();
	}

	public void zoomIn() {
		zoomFactor *= zoomStep;
		if (log.isInfoEnabled()) {
			log.info("Zooming in. Current zoom factor: " + zoomFactor);
		}
		drawFrame();
	}

	public void zoomOut() {
		zoomFactor /= zoomStep;
		if (log.isInfoEnabled()) {
			log.info("Zooming out. Current zoom factor: " + zoomFactor);
		}
		drawFrame();
	}

}
